const PENDING = 'pending';
const FULFILLED = 'fulfilled';
const REJECTED = 'rejected';

function Promisee(executor){
    this.state = PENDING;
    this.result = undefined;
    const _onChange = function (state,value){
        if(this.state !== PENDING) return;
        this.state = state;
        this.result = value;
    }
    try {
        executor(_onChange.bind(this,FULFILLED),_onChange.bind(this,REJECTED))
    }catch(err){
        _onChange.call(this,REJECTED,err)
    }

}


Object.assign(Promisee.prototype,{
    // onResolved:成功回调
    // onRejected:失败回调
    then(onResolved,onRejected){
        if(this.state === FULFILLED){
            setTimeout(()=>{
                onResolved(this.result);
            })
        }
        else if(this.state === REJECTED) {
            setTimeout(()=>{
                onRejected(this.result);
            })
        }
    },
    catch(){

    }
})
const p = new Promisee(function (resolve,reject){
    // resolve(100);
    // reject(200);
    throw 'xxx';
})

console.log(p);

